package ru.jexp.rcp.supply.swtext;

import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TreeColumn;

import ru.jexp.rcp.supply.beans.project.dvs.sostavizdelia.TreeSorter;

public interface IColumnCreator<ColumnT, ViewerT extends ColumnViewer>
{
	IColumnCreator<TreeColumn, TreeViewer> TREE = new IColumnCreator<TreeColumn, TreeViewer>()
	{
		@Override
		public TreeColumn createColumn(final TreeViewer viewer, String columnName, int width, int alignment)
		{
			final TreeColumn column = new TreeColumn(viewer.getTree(), SWT.NONE);
			column.setText(columnName);
			column.setToolTipText(columnName);
			column.setWidth(width);
			column.setAlignment(alignment);
			column.setResizable(true);
			column.setMoveable(viewer.getTree().getColumnCount() != 1);
			column.addSelectionListener(new SelectionAdapter()
			{
				@Override
				public void widgetSelected(SelectionEvent e)
				{
					TreeSorter comparator = (TreeSorter) viewer.getComparator();
					if (comparator != null)
					{
						comparator.changeDirecting();
						comparator.setSortingColumn(findIndexColumn(column));
						viewer.getTree().setSortDirection(comparator.getDirection());
						viewer.getTree().setSortColumn(column);
						viewer.refresh();
					}
				}

				private int findIndexColumn(TreeColumn column)
				{
					int count = viewer.getTree().getColumnCount();
					for (int j = 0; j < count; j++)
					{
						if (viewer.getTree().getColumn(j) == column)
						{
							return j;
						}
					}
					return 0;
				}
			});
			return column;
		}
	};

	IColumnCreator<TableColumn, TableViewer> TABLE = new IColumnCreator<TableColumn, TableViewer>()
	{
		@Override
		public TableColumn createColumn(final TableViewer viewer, String columnName, int width, int alignment)
		{
			final TableColumn column = new TableColumn(viewer.getTable(), SWT.NONE);
			column.setText(columnName);
			column.setToolTipText(columnName);
			column.setWidth(width);
			column.setAlignment(alignment);
			column.setResizable(true);
			column.setMoveable(viewer.getTable().getColumnCount() != 1);
			column.addSelectionListener(new SelectionAdapter()
			{
				@Override
				public void widgetSelected(SelectionEvent e)
				{
					TreeSorter comparator = (TreeSorter) viewer.getComparator();
					if (comparator != null)
					{
						comparator.changeDirecting();
						comparator.setSortingColumn(findIndexColumn(column));
						viewer.getTable().setSortDirection(comparator.getDirection());
						viewer.getTable().setSortColumn(column);
						viewer.refresh();
					}
				}

				private int findIndexColumn(TableColumn column)
				{
					int count = viewer.getTable().getColumnCount();
					for (int j = 0; j < count; j++)
					{
						if (viewer.getTable().getColumn(j) == column)
						{
							return j;
						}
					}
					return 0;
				}
			});
			return column;
		}
	};

	ColumnT createColumn(ViewerT viewer, String columnName, int width, int alignment);
}
